/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ibm.risk.irmp.common.auth.oauth2;

import com.ibm.risk.irmp.common.auth.oauth2.welink.OAuth2Configuration;
import com.ibm.risk.irmp.common.auth.oauth2.welink.WeLinkOAuth2SSOProcessor;
import com.ibm.risk.irmp.common.auth.sso.SSOProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OAuth2LoginConfig {
    public static final String OAUTH2_WELINK = "welink";
    //    @Bean
//    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//        http
//                .csrf(AbstractHttpConfigurer::disable)
//                .authorizeHttpRequests(authorize -> authorize
//                        .requestMatchers(AntPathRequestMatcher.antMatcher("/login/**"))
//                        .permitAll()
//                        .anyRequest().authenticated()
//                )
//                .sessionManagement( t -> t.sessionCreationPolicy(SessionCreationPolicy.STATELESS) )
//                .oauth2Login(withDefaults());
//        return http.build();
//    }

    @Bean
    public SSOProcessor ssoProcessor(@Value("${spring.security.oauth2.client.registration.provider}") String authProvider) {
        if (OAUTH2_WELINK.equals(authProvider)){
            return new WeLinkOAuth2SSOProcessor();
        }
        return new DefaultOAuth2SSOProcessor();
    }
    @Bean
    public OAuth2Configuration oAuth2Configuration() {
        return new OAuth2Configuration();
    }

    @Bean
    public SSOProcessor oAuth2SSOProcessor() {
        return new WeLinkOAuth2SSOProcessor();
    }

//    @Bean
//    public SSOFilter ssoFilter() {
//        return new SSOFilter();
//    }

}
